package logconf

import (
	rotate "github.com/lestrrat-go/file-rotatelogs"
	"github.com/sirupsen/logrus"
	"io"
	"path"
	"runtime"
	"strconv"
	"time"
)

var Logger = logrus.New()

func LogFormat(filename string) *logrus.Logger {
	Logger.SetFormatter(&logrus.JSONFormatter{
		CallerPrettyfier: func(frame *runtime.Frame) (function string, file string) { // 输出短路径文件名函数调用
			fileFun := path.Base(frame.Function)
			return fileFun, frame.File + strconv.Itoa(frame.Line)
		},
	})
	Logger.SetReportCaller(true)

	writer, _ := rotate.New(
		filename+".%Y%m%d%H%M",
		rotate.WithLinkName(filename),
		rotate.WithRotationTime(1*time.Hour),
	)
	writers := []io.Writer{writer}
	Logger.SetOutput(io.MultiWriter(writers...))
	return Logger
}
